// pages/ad/create.js
const user =require("../../lib/user.js")
Page({
  mixins: [require('../../lib/themeChanged')
    ,require('../../lib/request'),
    ,require('../../lib/validForm')],
  /**
   * 页面的初始数据
   */
  data: {
    endDate:'',
    startDate:''
    ,
    rules: [{
      name: 'name',
      rules: [{required: true, message: '活动标题必须填写'},{rangelength:[3,10],message:'标题长度不合适'}]
    },{
      name:"isAgree",
      rules:[
      {validator:function(rule, value, param, models){
        if(value==undefined || value.length<1){
          return "必须同意协议";
        }
      }}]
    }
    ],
    formData: {
    },
    disableSubmit:"disable"
  },
  formInputChange(e) {
      const {field} = e.currentTarget.dataset
      this.setData({
          [`formData.${field}`]: e.detail.value
      })
  },
    bindDateChange: function (e) {
      console.log(e)
      const {field} = e.currentTarget.dataset
      console.log(field)
      this.setData({
          [`${field}`]: e.detail.value,
          [`formData.${field}`]: e.detail.value
      })
  },
  reportChange: function (e) {
    const {field} = e.currentTarget.dataset
    this.setData({
        [`${field}`]: e.detail.value,
        [`formData.${field}`]: e.detail.value
    });
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
        radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
        radioItems: e.detail.value[0],
        [`formData.radio`]: e.detail.value[0]
    });
  },
  agreeChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
    const {field} = e.currentTarget.dataset
    this.setData({
        [`${field}`]: e.detail.value,
        [`formData.${field}`]: e.detail.value
    });
  },
  bindTimeChange: function (e) {
    this.setData({
        time: e.detail.value
    })
  },
  bindCountryCodeChange: function(e){
    console.log('picker country code 发生选择改变，携带值为', e.detail.value);

    this.setData({
        countryCodeIndex: e.detail.value
    })
  },
  bindCountryChange: function(e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);

    this.setData({
        countryIndex: e.detail.value
    })
  },
  bindAccountChange: function(e) {
    console.log('picker account 发生选择改变，携带值为', e.detail.value);
    this.setData({
        accountIndex: e.detail.value
    })
  },
  bindAgreeChange: function (e) {
    this.setData({
        isAgree: !!e.detail.value.length
    });
  },
  bindsuccess:function(e){
    this.setData({
      disableSubmit:false
    })
  },
  bindfail:function(e) {
    this.setData({
      disableSubmit:true
    })
  },
  submit:async function() {
    if(await this.validForm('#form')){
      const response=await this.request('ad','create',this.data.formData)
      console.log(response,'/pages/edit/uploadThumbnail?_id='+response._ids[0]);
      if(response.errMsg=="collection.add:ok"){
        wx.navigateTo({
          url: '/pages/edit/uploadThumbnail?_id='+response._ids[0],
        })
      }
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {title:'abc',url:'/pages/index/index'}
  }
})